$(function(){
    var Auth = new Services();
//    Auth.addService( new Vkontakte( 2287579 ) );
    Auth.init();
});

$(function(){

    VK.init({
        apiId: 2287579
    });

    $('a#vk-login').click(function(event){
        event.preventDefault();
        VK.Auth.login(null, VK.access.FRIENDS);
        return false;
    });

    $('a#vk-logout').click(function(event){
        event.preventDefault();
        VK.Auth.logout(function(){
            refreshPage(false);
        });
    });

//                VK.Observer.subscribe('auth.login', function(response){
//                    refreshPage(true);
//                });
    VK.Observer.subscribe('auth.sessionChange', function(response){
//        refreshPage(true);
    });



    var isLogged = false;
    function refreshPage(checkLogged)
    {
        if((checkLogged && !isLogged) || !checkLogged)
        {
            isLogged = true;
            location.href = 'index.php';
        }
    }

    VK.Auth.getLoginStatus(function(response){
        if(response.session)
        {
            VK.Api.call('friends.get', {
                fields: 'photo_rec'
            }, function(data){
                if(!data.error)
                {
                    if(data.response.length > 0)
                    {
                        data.response.shuffle();
                        for(i = 0; i < data.response.length; i++)
                        {
                            var friend = data.response[i];
                            if(friend.photo_rec && friend.photo_rec.indexOf('images/question_c.gif') == -1)
                            {
                                photos.push({
                                    photo: friend.photo_rec,
                                    id: friend.uid
                                });
                            }
                        }
                    }
                    initGameField();
                }
                else
                {
                    if(data.error.error_code == 7)
                    {
                        isLogged = false;
                        $('div#login-bar').show();
                        $('div#logout-bar').hide();
                        $.unblockUI();
                    }
                    else
                    {
                        showError(data.error.error_msg);
                    }
                }
            });
        }
        else
        {
            // пользователь не авторизирован
        }
    });

});


function Services()
{

}
Services.obj = {};
Services.prototype.services = [];
Services.prototype.service = null;

Services.prototype.init = function()
{
    Services.obj = this;

    // BEGIN TEMP
    this.addService( new Vkontakte( 2287579 ) );
    // END TEMP

    $("a.auth_services").bind('click', function( event ){
        event.preventDefault();
        Services.obj.selectService( $(this).attr('href') );
    });

};

Services.prototype.addService = function( $service )
{
    this.services[$service.getType()] = $service;
};

Services.prototype.selectService = function( type )
{
    if( !this.services[type] )
    {
        throw new Exception( "Not added service by type " + type );
    }
    this.service = this.services[type];
    this.serviceInit();
};

Services.prototype.serviceInit = function( )
{
    this.service.init();
    this.service.initLogin();
    this.service.initLogout();
};


/**
 * Инициализация выхода
 */
Services.prototype.initLogout = function( )
{
    this.service.initLogout();
};